package com.ptswitch.material.components;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.base.AbstractConditional;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.util.OgnlUtil;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;

public class IfContract extends AbstractConditional {
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String admin;

    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String department;

    @Parameter
    private Uzer user;

    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String contractStatus;

    @Parameter
    private Contract contract;

    @Inject
    private ComponentResources componentResources;

    @Override
    protected boolean test() {
        if (contractStatus != null) {
            if (!contractStatus.equals(contract.getStatus())) {
                return false;
            }
        }
        Uzer visitor = (Uzer) OgnlUtil.getValue(componentResources.getPage(), "visitor");
        if (user != null) {
            return user.getUsername().equals(visitor.getUsername());
        }
        if (admin != null) {
            int adminGrade = visitor.getAdminGrade();
            if (admin.equals("admin")) {
                if (adminGrade > Uzer.Power_Admin) {
                    return false;
                }
            } else if (admin.equals("admins")) {
                if (adminGrade > Uzer.Power_Admins) {
                    return false;
                }
            } else if (admin.equals("login")) {
                if (adminGrade > Uzer.Power_Normal) {
                    return false;
                }
            }
        }
        if (department != null) {
            int department2 = visitor.getDepartment();
            if (department.contains("contract")) {
                if (department2 == Uzer.Department_Buyer || department2 == Uzer.Department_Financial || department2 == Uzer.Department_Production || department2 == Uzer.Department_ContractAuditor) {
                    return true;
                }
            }
            if (department.contains("financial")) {
                if (department2 == Uzer.Department_Financial) {
                    return true;
                }
            }
            if (department.contains("buyer")) {
                if (department2 == Uzer.Department_Buyer) {
                    return true;
                }
            }
            if (department.contains("production")) {
                if (department2 == Uzer.Department_Production) {
                    return true;
                }
            }
            if (department.contains("auditor")) {
                if (department2 == Uzer.Department_ContractAuditor) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }

}
